//-----------------------------------------------------------------------
//<copyright file="PhieuXuatDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class PhieuXuatDAO : SQLDatabase, IPhieuXuatDAO
	{

		#region Create PhieuXuat methods

		public PhieuXuat CreatePhieuXuat(PhieuXuat phieuXuat)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuXuatInsert");
				AddOutParameter(command, "@SoPX", SqlDbType.NVarChar, phieuXuat.SoPX);
				AddInParameter(command, "@NgayXuatKho", SqlDbType.DateTime, phieuXuat.NgayXuatKho);
				AddInParameter(command, "@NguoiXuat", SqlDbType.NVarChar, phieuXuat.NguoiXuat);
				AddInParameter(command, "@NgayLapPhieu", SqlDbType.DateTime, phieuXuat.NgayLapPhieu);
				AddInParameter(command, "@NguoiLap", SqlDbType.NVarChar, phieuXuat.NguoiLap);
				AddInParameter(command, "@SoHD", SqlDbType.NVarChar, phieuXuat.SoHD);
				AddInParameter(command, "@LyDoXuat", SqlDbType.NVarChar, phieuXuat.LyDoXuat);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, phieuXuat.GhiChu);
				AddInParameter(command, "@MaKH", SqlDbType.NVarChar, phieuXuat.MaKH);
				AddInParameter(command, "@MaKho", SqlDbType.NVarChar, phieuXuat.MaKho);

				ExecuteNonQuery(command);

                phieuXuat.SoPX = (string)GetParameterValue(command,"@SoPX");
				return phieuXuat;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreatePhieuXuatException", ex);
			}
		}

		#endregion

		#region Update PhieuXuat methods

		public void UpdatePhieuXuat(PhieuXuat phieuXuat)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuXuatUpdate");

				AddInParameter(command, "@SoPX", SqlDbType.NVarChar, phieuXuat.SoPX);
				AddInParameter(command, "@NgayXuatKho", SqlDbType.DateTime, phieuXuat.NgayXuatKho);
				AddInParameter(command, "@NguoiXuat", SqlDbType.NVarChar, phieuXuat.NguoiXuat);
				AddInParameter(command, "@NgayLapPhieu", SqlDbType.DateTime, phieuXuat.NgayLapPhieu);
				AddInParameter(command, "@NguoiLap", SqlDbType.NVarChar, phieuXuat.NguoiLap);
				AddInParameter(command, "@SoHD", SqlDbType.NVarChar, phieuXuat.SoHD);
				AddInParameter(command, "@LyDoXuat", SqlDbType.NVarChar, phieuXuat.LyDoXuat);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, phieuXuat.GhiChu);
				AddInParameter(command, "@MaKH", SqlDbType.NVarChar, phieuXuat.MaKH);
				AddInParameter(command, "@MaKho", SqlDbType.NVarChar, phieuXuat.MaKho);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdatePhieuXuatException", ex);
			}
		}

		#endregion

		#region Delete PhieuXuat methods

		public void DeletePhieuXuat(PhieuXuat phieuXuat)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuXuatDelete");

				AddInParameter(command, "@SoPX", SqlDbType.NVarChar, phieuXuat.SoPX);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeletePhieuXuatException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll PhieuXuat methods

		public PhieuXuats GetAllPhieuXuat()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuXuatGetList");
				PhieuXuats Result = new PhieuXuats();
				List<PhieuXuat> List = ExecuteQuery<PhieuXuat>(command,true);
				foreach(PhieuXuat item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllPhieuXuatException", ex);
			}
		}

        public PhieuXuats GetAllPhieuXuatForPhieuGiao(bool isHavePhieuGiao)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuXuatGetListByPhieuGiao");
                AddInParameter(command, "@HavePhieuGiao", SqlDbType.NVarChar, isHavePhieuGiao);
                PhieuXuats Result = new PhieuXuats();
                List<PhieuXuat> List = ExecuteQuery<PhieuXuat>(command, true);
                foreach (PhieuXuat item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllPhieuXuatException", ex);
            }
        }

		#endregion


		#region GetPhieuXuat methods

		public PhieuXuat GetPhieuXuatByID(string soPX)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuXuatGet");

				AddInParameter(command, "@soPX", SqlDbType.NVarChar, soPX);

				return ExecuteQuery<PhieuXuat>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetPhieuXuatBy ID Exception", ex);
			}
		}

		#endregion

        #region GetPhieuXuatByDate methods

        public PhieuXuats GetPhieuXuatByDate(DateTime? tungay, DateTime? denngay)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spGetPhieuXuatListByDate");

                AddInParameter(command, "@tungay", SqlDbType.DateTime, tungay);
                AddInParameter(command, "@denngay", SqlDbType.DateTime, denngay);

                PhieuXuats Result = new PhieuXuats();
                List<PhieuXuat> List = ExecuteQuery<PhieuXuat>(command, true);
                foreach (PhieuXuat item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetPhieuNhapBy ID Exception", ex);
            }
        }

        #endregion

		#endregion
	}
}//End Class
